/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 *
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.core.command.invoker.processor;

import static junit.framework.Assert.*;
import static org.easymock.EasyMock.*;

import org.junit.Test;
import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.mock.AnnotatedCommand;
import org.openinsula.vulcano.core.command.mock.ConfigurableCommand;
import org.openinsula.vulcano.core.command.mock.InitializingBeanCommandInterface;
import org.openinsula.vulcano.core.command.mock.PostConstructCommandInterface;
import org.openinsula.vulcano.core.command.processor.CommandChain;
import org.openinsula.vulcano.core.command.processor.CommandProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

@ContextConfiguration
public class DependencyInjectionCommandProcessorTestCase extends AbstractJUnit4SpringContextTests {

	@Autowired
	private CommandProcessor commandProcessor;

	private final CommandChain commandChain = new CommandChain() {
		@Override
		public <T> T doChain(Command<T> command) {
			return command.execute();
		}
	};

	@Test
	public void testFullDependencyInjection() {
		assertTrue(commandProcessor.doExecute(new AnnotatedCommand(), commandChain));
		assertTrue(commandProcessor.doExecute(new ConfigurableCommand(), commandChain));
	}

	@Test
	public void testInitializingBeanCommand() throws Exception {
		InitializingBeanCommandInterface mockCommand = createMock(InitializingBeanCommandInterface.class);
		mockCommand.afterPropertiesSet();
		expect(mockCommand.execute()).andReturn(null);
		replay(mockCommand);

		assertNull(commandProcessor.doExecute(mockCommand, commandChain));
	}

	@Test
	public void testPostConstructCommand() {
		PostConstructCommandInterface mockCommand = createMock(PostConstructCommandInterface.class);
		mockCommand.init();
		expect(mockCommand.execute()).andReturn(null);
		replay(mockCommand);

		assertNull(commandProcessor.doExecute(mockCommand, commandChain));
	}

}